home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: link-1.ts.bcc.ac.uk!ucecb09
- From: ucecb09@ucl.ac.uk (Robert Byrne)
- Subject: Re: Friend vs. Member function for operator overload
- Message-ID: <1996Apr15.104234.55653@ucl.ac.uk>
- Date: Mon, 15 Apr 1996 10:42:34 GMT
- References: <4kj55n$mv2@homenet.hom.net> <316E6F5E.2F1C@lmf-di.puc-rio.br>
- Organization: University College London
-
- Paulo Eduardo Neves <neves@lmf-di.puc-rio.br> writes:
-
- >Daniel P. Engel III wrote:
- >>
- >> Can anyone tell me if one of these the "preferred" way of doing it?
- >> And, what are the advantages and disadvantages of each?
- >>
-
- >When you use a friend function your code becomes more consistent, since
- >the same conversions can be applied to both arguments. See a operator+
- >defined as friend in a string class that has a ctor from a standard c
- >string :
-
- However, there's usually no need to make arithmetic operators friends
- as they can simply call the appropriate assignment op. To whit;
-
- struct String
- {
- String &operator+=(const String &);
- String(const String&);
- String(const char*);
- //...
- };
-
- String operator+(const String & lhs, const String &rhs)
- {
- return String(lhs) += rhs;
- }
-
- Giving the advantage of symmetry for the args but w/o adding friends
- and keeping all the code within the class.
-
- Rob
-
- --
- Robert Byrne `I think therefore I am' says rather more than
- ucecb09@ucl.ac.uk is strictly certain.' -Bertrand Russell
-
-